<?php

namespace App\Http\Middleware;

use Closure;
use Exception;
use Illuminate\Http\Request;

/**
 * 禁止某些IP访问API接口
 * Class BanSpecificIp
 * @package App\Http\Middleware
 */
class BanSpecificIp extends Middleware
{
    private $bannedIps = ['123.123.123.123', '127.0.0.1']; // 这里填写要封禁的IP地址

    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     * @throws Exception
     */
    public function handle(Request $request, Closure $next)
    {
        if (in_array($request->ip(), $this->bannedIps)) {
            // 返回一个403错误响应
            $this->returnError(RETURN_FAIL,'Your IP address is banned.');
        }

        return $next($request);
    }
}
